package com.alan.camunda.boot.delegate;

import com.alan.camunda.boot.util.CacheableExpressionManager;
import com.alan.camunda.boot.util.ExpressionManagerHolder;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.impl.el.Expression;
import org.camunda.bpm.engine.variable.Variables;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 使用 Engine 的 ExpressionManager 计算表达式
 */
@Slf4j
@Component("switchDelegate")
public class SwitchDelegate implements JavaDelegate {
    @Resource
    private RuntimeService runtimeService;

    @Override
    public void execute(DelegateExecution execution) throws Exception {


        //execution.setVariable("result", 4);
        //execution.setVariable("mm", 4);
        // 抛出异常前，对输出的值做赋值
       // execution.setVariable("mm", 1);

//        JSONObject r = new JSONObject();
//        r.put("data", "333");
//
//        execution.setVariableLocal("_R", Variables.objectValue(r, true));
//
//
//        Object value = ExpressionManagerHolder.get().getExpression("${_R.data}").getValue(execution);
//        execution.setVariable("outVar1", value);

        execution.setVariable("WebService_R", "Success");
        String id = execution.getProcessInstance().getSuperExecution().getId();
        runtimeService.setVariable(id, "bySub", "sddd");
        throw new BpmnError("error condition");
    }



}
